package com.pri.chat.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.PayResultBean;
import com.netease.nim.uikit.net.entity.RechargeBean;
import com.netease.nim.uikit.net.entity.RechargeBondBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.RechargeListAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.model.RechargePayModel;
import com.pri.chat.model.UserModel;
import com.pri.chat.pay.PayListenerUtils;
import com.pri.chat.pay.PayResultListener;
import com.pri.chat.pay.PayUtils;
import com.pri.chat.utils.Des3Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/23
 */
public class RechargeListActivity extends BaseActivity implements PayResultListener {
    @BindView(R.id.goldNumber)
    TextView goldNumber;
    @BindView(R.id.wxRelation)
    View wxRelation;
    @BindView(R.id.wxImage)
    ImageView wxImage;
    @BindView(R.id.zfbRelation)
    View zfbRelation;
    @BindView(R.id.zfbImage)
    ImageView zfbImage;

    @BindView(R.id.rec_recharge_list)
    RecyclerView recRecharge;

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            RechargeListActivity.this.setResult(2);
            finish();
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge_layout;
    }

    @Override
    protected void initView() {
        setTitle("金币充值");
        PayListenerUtils.getInstance(this).addListener(this);
        IntentFilter intentFilter = new IntentFilter("PaySuccess");
        registerReceiver(broadcastReceiver, intentFilter);
        initRv();
        initPrice();
    }
    private List<RechargeBondBean> mlist = new ArrayList<>();
    private RechargeListAdapter mAdapter;
    private int mPosition;
    private void initRv() {
        mlist = new ArrayList<>();
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 3);
        recRecharge.setLayoutManager(linearLayoutManager);
        recRecharge.setNestedScrollingEnabled(false);
        mAdapter = new RechargeListAdapter(R.layout.item_recharge_bond, mlist);
        recRecharge.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mPosition = position;
            for (int i = 0; i < mlist.size(); i++) {
                if (i == position) {
                    mlist.get(position).setCheck(true);
                } else {
                    mlist.get(i).setCheck(false);
                }
            }
            mAdapter.notifyDataSetChanged();

        });
    }


    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<RechargeBondBean>>> onNextListener = baseBean -> {
            mlist.addAll(baseBean.getData());
            mlist.get(0).setCheck(true);
            mAdapter.notifyDataSetChanged();
        };
        HttpMethods.getInstance().listChargeGold(new ProgressSubscriber<>(onNextListener, this, true));
    }

    private void initPrice() {
        SubscriberOnNextListener<BaseBean<RechargeBean>> onNextListener = baseBean -> {
            goldNumber.setText(baseBean.getData().getGoldNumber());
        };
        UserModel regModel = new UserModel();//登录上传参数
        regModel.setMemberId(SharedHelper.readId(this));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().getWalletInfo(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }

    @Override
    public void onPaySuccess() {
        RechargeListActivity.this.setResult(2);
        finish();
    }

    @Override
    public void onPayError() {

    }

    @Override
    public void onPayCancel() {

    }

    private int payType = 1;
    @OnClick({R.id.tv_pay,R.id.wxRelation,R.id.zfbRelation})
    public void onViewClicked(View v) {
        if(R.id.tv_pay == v.getId()){
            httpBuy(mlist.get(mPosition).getId());
        }else if(v.getId() == R.id.wxRelation){
            payType = 1;
            wxRelation.setBackgroundResource(R.drawable.btn_orange_noconer_line_shape);
            wxImage.setVisibility(View.VISIBLE);
            zfbRelation.setBackgroundResource(R.drawable.btn_gray_noconer_line_shape);
            zfbImage.setVisibility(View.GONE);
        }else if(v.getId() == R.id.zfbRelation){
            payType = 0;
            wxRelation.setBackgroundResource(R.drawable.btn_gray_noconer_line_shape);
            wxImage.setVisibility(View.GONE);
            zfbRelation.setBackgroundResource(R.drawable.btn_orange_noconer_line_shape);
            zfbImage.setVisibility(View.VISIBLE);
        }

    }

    private void httpBuy(String id) {
        SubscriberOnNextListener<BaseBean<PayResultBean>> onNextListener = baseBean -> {
            if (payType == 0) {
                PayUtils.getInstance(this).alipay(PayUtils.PAY_TYPE_ALI, baseBean.getData().getAlipay());
            } else {
                PayUtils.getInstance(this).wxpay(PayUtils.PAY_TYPE_WX, baseBean.getData().getWxpay());
            }
        };
        RechargePayModel rechargePayModel = new RechargePayModel();//登录上传参数
        rechargePayModel.setMemberId(SharedHelper.readId(this));
        rechargePayModel.setObjId(id);
        rechargePayModel.setPayType(payType);
        rechargePayModel.setType(0);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(rechargePayModel));
        HttpMethods.getInstance().createOrder(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
        PayListenerUtils.getInstance(this).removeListener(this);
    }
}
